<template>
  <div class="app-container">
    <el-card class="box-card search-div">
      <el-form :inline="true">
        <el-form-item label="任务名称">
          <el-input placeholder="请输入查询的任务名称" v-model="searchName"/>
        </el-form-item>
        <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
        <el-button @click="handleReset" icon="el-icon-refresh">重置</el-button>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <el-table :data="unSendList" v-loading.body="unSendListLoading" border fit highlight-current-row class="table-list">
          <el-table-column align="center" label="任务名称" prop="name"/>
          <el-table-column align="center" label="流程名称" prop="processDefinitionName" width="180"/>
          <el-table-column align="center" label="流程标识" prop="processDefinitionKey" width="180"/>
          <el-table-column align="center" label="流程版本" prop="processVersion" width="100">
              <template slot-scope="scope">
                  <el-tag>v{{scope.row.processVersion}}</el-tag>
              </template>
          </el-table-column>
          <el-table-column align="center" label="创建时间" prop="startTime" width="150"/>
          <el-table-column align="center" label="操作" width="240" fixed="right">
              <template slot-scope="scope">
              <el-button v-if="permissions.flowable_unsendedTask_send" type="primary" @click="sendFirstTask(scope.row.taskId)">提交</el-button>
              <el-button v-if="permissions.flowable_task_detail" @click="showDetail(scope.row.taskId,scope.row.id)">详情</el-button>
              <el-button v-if="permissions.flowable_diagram" type="info" @click="viewDiagram(scope.row)">流程图</el-button>
              </template>
          </el-table-column>
      </el-table>
      <div class="pagination-container">
          <el-pagination background @size-change="handleUnSendSizeChange" @current-change="handleUnSendCurrentChange" :current-page="listUnSendQuery.page"
              :page-sizes="[10,20,30,50]" :page-size="listUnSendQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="unSendTotal">
          </el-pagination>
      </div>
    </el-card>
    <el-dialog title="流程详情" :width="'800px'" center :visible.sync="showDialog" :close-on-click-modal="false">
      <el-form ref="flowform" label-width="80px">
        <FormBuildTCD ref="fbt" :options="formOptions" :defaultValue="variables"/>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog title="流程图" :width="'800px'" :visible.sync="showDialogDiagram">
        <img :src="flowableImage" v-if="flowableImage" class="flowableImage">
    </el-dialog>
  </div>
</template>

<script>
import * as task from './js/task'
import * as flowablePublish from '../flow/flowPublish'
import * as flowbleForm from '../flow/flowForm'
import { mapGetters } from 'vuex'

export default {
  name: 'unsendTaskList',
  data() {
    return {
      unSendList: null,
      unSendListLoading: false,
      unSendTotal: 0,
      searchName: '',
      showDialog: false,
      listUnSendQuery: {
        page: 1, limit: 10, name: '', status: 0
      },
      formOptions:{
        config: {
        labelPosition: "right",
        labelWidth: 15,
        },
        list: []
      },
      variables: {}
      ,showDialogDiagram:false
      ,flowableImage:''
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
    this.getUnSendList()
  },
  methods: {
    getUnSendList() {
      this.unSendListLoading = true
      this.listUnSendQuery.name = this.searchName
      task.myselfTaskList(this.listUnSendQuery).then(response => {
        this.unSendList = response.data.records
        this.unSendTotal = response.data.total
        this.unSendListLoading = false
      })
    },
    handleSearch() {
      this.listUnSendQuery.page = 1
      this.getUnSendList()
    },
     handleReset(){
      this.searchName = ''
       this.listUnSendQuery = {
          page: 1, limit: 10, name: '', status: 0
      };
      this.getUnSendList()
    },
    handleUnSendSizeChange(val) {
        this.listUnSendQuery.limit = val
        this.getUnSendList()
    },
    handleUnSendCurrentChange(val) {
        this.listUnSendQuery.page = val
        this.getUnSendList()
    },
    //发送
    sendFirstTask(taskId){
       this.$confirm('确认要提交此流程', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        task.sendFirstTask(taskId).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getUnSendList()
        })
      }).catch(function(error) { console.log(error) })
    },
    //详情
    showDetail(taskId,processInstanceId){
      flowableForm.getFormInfoByTaskIdAndProcessInstanceId(taskId,processInstanceId).then(resp => {
        this.showDialog = true
         this.formOptions = JSON.parse(resp.data.formModel)
         this.variables = resp.data.variables
      });
    },
    //流程图
    viewDiagram(row){
      this.showDialogDiagram = true
       flowablePublish.diagram(row.processDefinitionKey,row.id).then((response)=>{
            this.flowableImage = response.data
        })
    }
  }
}
</script>